clear all
set more off, perm
set maxvar 120000
set varabbrev off
* ---------------------------------------------- *
global dir 	"`1'"
global Data 	$dir/Data
global Tables 	$dir/Tables
global Figures 	$dir/Figures
global Work 	$dir/Work
global Temp 	$dir/Temp
global Pseudo	$dir/Work/Temp
* ---------------------------------------------- *
use if qruf1<. using $Work/EBR_new_monthly, clear

tsset permno datem

* compute actual returns
generate   retQr1=1
forvalues x=1(1)5 {
generate   retYr`x'=1
}

generate  retMo1=ret_mo1
forvalues x=1(1)3 {
replace   retQr1=retQr1*(ret_mo`x')
}
forvalues x=1(1)12 {
replace   retYr1=retYr1*(ret_mo`x')
}
forvalues x=13(1)24 {
replace   retYr2=retYr2*(ret_mo`x')
}
forvalues x=25(1)36 {
replace   retYr3=retYr3*(ret_mo`x')
}
forvalues x=37(1)48 {
replace   retYr4=retYr4*(ret_mo`x')
}
forvalues x=49(1)60 {
replace   retYr5=retYr5*(ret_mo`x')
}
* *************************
scalar alfa= 0.97749304
gen cumRetNewMo1=ln(retMo1) 
gen cumRetNewQr1=ln(retQr1) 
gen cumRetNewYr1=ln(retYr1) 
gen cumRetNewYr3=ln(retYr1)+alfa*ln(retYr2)+alfa^2*ln(retYr3) 
gen cumRetNewYr5=ln(retYr1)+alfa*ln(retYr2)+alfa^2*ln(retYr3)+alfa^3*ln(retYr4)+alfa^4*ln(retYr5)
* **************************
gen cumQNRetMo1=ln(1+qnic1 ) 
gen cumQNRetQr1=ln(qnic_qr1) 
gen cumQNRetYr1=ln(qnic_yr1) 
gen cumQNRetYr3=ln(qnic_yr1)+alfa*ln(qnic_yr2)+alfa^2*ln(qnic_yr3) 
gen cumQNRetYr5=ln(qnic_yr1)+alfa*ln(qnic_yr2)+alfa^2*ln(qnic_yr3)+alfa^3*ln(qnic_yr4)+alfa^4*ln(qnic_yr5)
gstats winsor cumQN*, cuts(1 99) by(datem) replace 
* **************************
keep permco permno datem cum*
tempfile cumQ
save "`cumQ'", replace
* **************************
merge 1:1 permco permno datem using $Work/Firms01_June
keep if _merge==3
drop _merge
drop *LTG LTG
* *****************************************************
merge 1:1 permco permno statpers using  $Work/XSibes.dta, keepusing(permco permno statpers LTG datem)

tsset permno datem

replace LTG=LTG/100
gen L1LTG=L12.LTG

gen dMFLTG=F1.LTG-LTG
gen dQFLTG=F3.LTG-LTG

forvalues x=1(1)5 {
	local j=12*`x'
	gen d`x'FLTG=F`j'.LTG-LTG
	gen d`x'LLTG=LTG-L`j'.LTG
}
drop _merge
* *****************************************************
keep if BOOK+SIZE+LTG<.
* *****************************************************
foreach var of varlist FE1 FE3 FE5 cp* d*FLTG {
	quietly: bysort datem: egen max=pctile(`var'), p(99)
	quietly: bysort datem: egen min=pctile(`var'), p(1)  
	quietly: replace `var'=max if `var'>max & `var'<.
	quietly: replace `var'=min if `var'<min	
	drop max min 
}
foreach var of varlist FE1 FE3 FE5 cp1 cp3 cp5 d1FLTG d3FLTG d5FLTG {
	quietly: egen sd=sd(`var')
	quietly: replace `var'=`var'/sd	
	drop sd
}

eststo clear

generate FE=FE1
generate FR=cp1
generate LT=d1FLTG

preserve
keep if FE+FR+LT+cumRetNewYr1<.
eststo:  quietly: ivreghdfe cumRetNewYr1 FE FR LT, dkraay(13) a(datem permco)
eststo:  quietly: ivreghdfe cumRetNewYr1 FE FR LT, dkraay(13) a(datem       ) 
eststo:  quietly: ivreghdfe cumRetNewYr1 FE FR LT, dkraay(13)
restore

replace  FE=FE3
replace  FR=cp3
replace  LT=d3FLTG

preserve
keep if FE+FR+LT+cumRetNewYr3<.
eststo:  quietly: ivreghdfe cumRetNewYr3 FE FR LT, dkraay(37) a(datem permco)
eststo:  quietly: ivreghdfe cumRetNewYr3 FE FR LT, dkraay(37) a(datem       ) 
eststo:  quietly: ivreghdfe cumRetNewYr3 FE FR LT, dkraay(37)
restore

replace  FE=FE5
replace  FR=cp5
replace  LT=d5FLTG

preserve
keep if FE+FR+LT+cumRetNewYr5<.
eststo:  quietly: ivreghdfe cumRetNewYr5 FE FR LT, dkraay(61) a(datem permco)
eststo:  quietly: ivreghdfe cumRetNewYr5 FE FR LT, dkraay(61) a(datem       ) 
eststo:  quietly: ivreghdfe cumRetNewYr5 FE FR LT, dkraay(61)
restore
* *****************************************************
local file "$Tables/AppendixB9_June.csv"
esttab using `file', nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap stats(N r2 r2_a) order(FE FR LT) replace
